package demo;


import demo.dao.UserMapper;
import jdbc.DBUtil;
import jdbc.InputUtil;
import jdbc.UserInfo;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * 控制器根据用户不同需求调用不同业务操作
 */
public class UserController {

    public void start() {
        System.out.println("-------------欢迎使用用户管理系统-------------");
        System.out.println("请输入要进行操作的编号");
        System.out.println("[1]:用户注册");
        System.out.println("[2]:用户登录");
        System.out.println("[3]:修改密码");
        Scanner sc = new Scanner(System.in);
        System.out.println("-----请选择编号-----");
        String num = sc.nextLine();
        switch (num) {
            case "1":
                try {
                    rehUser();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                break;
            case "2":
                loginUser();
                break;
            case "3":
                try {
                    changePWD();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                break;
            default:
                System.out.println("输入的编号有误！");
        }
    }

    //用户注册功能
    public void rehUser() throws SQLException {
        //用户名不能重复
        UserInfo userInfo = InputUtil.getInputObject(new UserInfo(), "欢迎注册");

        UserMapper userMapper = new UserMapper();
        if (userMapper.findByUsername(userInfo.getUsername()) != null) {
            System.out.println("该用户已存在");
        } else {
            int i = userMapper.insert(userInfo);
            if (i > 0) {
                System.out.println("注册成功");
            } else {
                System.out.println("注册失败");
            }

        }

    }
    //用户登录功能
    public void loginUser() {
        UserInfo userInfo = InputUtil.getInputObject(new UserInfo(), "欢迎登录", "登录");
        UserMapper userMapper = new UserMapper();
        UserInfo byUsernamePassword = userMapper.findByUsername_password(userInfo.getUsername(), userInfo.getPassword());
        if (byUsernamePassword != null) {
            System.out.println("登陆成功,欢迎回来"+byUsernamePassword.getNickname());
        } else {
            System.out.println("登陆失败");
        }
    }

    //修改密码功能
    public void changePWD() throws SQLException {

        //输入新密码
        Scanner sc = new Scanner(System.in);
        System.out.println("输入用户名");
        String username = sc.nextLine();
        System.out.println("输入密码");
        String oldpassword = sc.nextLine();
        System.out.println("输入新密码");
        String newpassword = sc.nextLine();

        UserMapper userMapper = new UserMapper();
        //判断用是否存在
        UserInfo userInfo = userMapper.findByUsername(username);
        if (userInfo==null ||!userInfo.getPassword().equals(oldpassword)) {
            System.out.println("输入错误");
        } else {
            userInfo.setPassword(newpassword);
            userMapper.update(userInfo);
            System.out.println("修改成功");
        }
    }

    public static void main(String[] args) {
        UserController userController = new UserController();
        userController.start();

    }


}
